﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Abp.UI;

namespace Camc.OutsourceProcession.Printer
{
	public static class PrintForEquipment
	{
		public static string USER = "lixiaopeng_dl@hotmail.com";  //*必填*：登录管理后台的账号名
		public static string UKEY = "aPefHmcXZHTPTyBV";//*必填*: 注册账号后生成的UKEY
		public static string URL = "http://api.feieyun.cn/Api/Open/";//不需要修改

		//打印机编号，指定具体打印机
		public static string SN = "";        //*必填*：打印机编号，必须要在管理后台里手动添加打印机或者通过API添加之后，才能调用API

		public static string Print(string sn,string QRcode, string equipmentName)
		{
			
			SN = sn;

			if (SN == "")
			{
				throw new UserFriendlyException("未绑定打印机");
			}
			string orderInfo = string.Empty;
			orderInfo += "<QR x=\"10\"  y=\"100\"  e=\"L\"  w=\"7\">" + QRcode + "</QR>";
			orderInfo += "<TEXT x=\"100\" y=\"30\" font=\"12\" w =\"1\" h=\"2\" r=\"0\"> 工位码</TEXT>";
			orderInfo += "<TEXT x=\"230\" y=\"100\" font=\"12\" w=\"1\" h=\"2\" r=\"0\">" + equipmentName + "</TEXT>";

			orderInfo = Uri.EscapeDataString(orderInfo);
			HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URL);
			req.Method = "POST";
			UTF8Encoding encoding = new UTF8Encoding();

			string postData = "sn=" + SN;
			postData += ("&content=" + orderInfo);
			postData += ("&times=" + "1");//默认1联

			int itime = DateTimeToStamp(System.DateTime.Now);//时间戳秒数
			string stime = itime.ToString();
			string sig = sha1(USER, UKEY, stime);

			//公共参数
			postData += ("&user=" + USER);
			postData += ("&stime=" + stime);
			postData += ("&sig=" + sig);
			postData += ("&apiname=" + "Open_printLabelMsg");

			byte[] data = encoding.GetBytes(postData);

			req.ContentType = "application/x-www-form-urlencoded";
			req.ContentLength = data.Length;
			Stream resStream = req.GetRequestStream();

			resStream.Write(data, 0, data.Length);
			resStream.Close();

			HttpWebResponse response;
			string strResult;
			try
			{
				response = (HttpWebResponse)req.GetResponse();
				StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
				strResult = reader.ReadToEnd();
			}
			catch (WebException ex)
			{
				response = (HttpWebResponse)ex.Response;
				strResult = response.StatusCode.ToString();//错误信息
			}

			response.Close();
			req.Abort();
			//服务器返回的JSON字符串，建议要当做日志记录起来
			return strResult;
		}

		//签名USER,UKEY,STIME
		public static string sha1(string user, string ukey, string stime)
		{
			var buffer = Encoding.UTF8.GetBytes(user + ukey + stime);
			var data = SHA1.Create().ComputeHash(buffer);

			var sb = new StringBuilder();
			foreach (var t in data)
			{
				sb.Append(t.ToString("X2"));
			}

			return sb.ToString().ToLower();

		}


		private static int DateTimeToStamp(System.DateTime time)
		{
			System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds;
		}
	}
}
